In [1]:
import pandas as pd 
#import plotly_express as px
import plotly.express as px
In [2]:
d = {'country': ["Argentina", "Argentina", "Argentina", "Argentina", "Argentina", "Argentina", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany"],
     'year': [2005, 2006, 2007, 2008, 2009, 2010, 2005, 2006, 2007, 2008, 2009, 2010],
     'education': [3.860, 4.128, 4.462, 4.844, 5.531, 5.019, 4.134, 4.279, 4.343, 4.41, 4.88, 4.914],
     'trade': [34.739, 34.700, 34.947, 35.258, 28.367, 29.502, 64.143, 67.105, 69.076, 70.122, 59.871, 67.711]
    }

dataset = pd.DataFrame(data = d)
dataset.head()
Out[2]:
country year education trade
0 Argentina 2005 3.860 34.739
1 Argentina 2006 4.128 34.700
2 Argentina 2007 4.462 34.947
3 Argentina 2008 4.844 35.258
4 Argentina 2009 5.531 28.367
In [3]:
# Take only the rows with Argentina
#datasetArgentina = dataset.query("country == 'Argentina'")
#px.line(datasetArgentina, x="year", y="trade", line_group="country")

fig = px.line(dataset, x="year", y="trade", line_group="country", color="country", template="plotly_white",
       color_discrete_map = dict(Argentina = "#4daf4a", Germany = "#377eb8"),
        labels = dict(year = "Year", trade = "Merchandise Trade (% of GDP)"),
        range_y = [0, 100],
        render_mode = 'svg'
       )


fig.update(layout = dict(
                    legend = dict(orientation = 'h', y = 1.1, x = 0.5)
                    ))
In [4]:
# Trade on scatterplot
fig = px.scatter(dataset, x="year", y="trade", color="country", template="plotly_white",
       color_discrete_map = dict(Argentina = "#4daf4a", Germany = "#377eb8"),
        labels = dict(year = "Year", trade = "Merchandise Trade (% of GDP)"),
        range_y = [0, 100],
        render_mode = 'svg'
       )


fig.update(layout = dict(
                    legend = dict(orientation = 'h', y = 1.1, x = 0.5)
                    ))
In [5]:
# Trade on world map
# Create an animated choropleth map 

px.choropleth(dataset, locationmode="country names", locations="country",
              color = "trade", hover_name = "country",
           animation_frame = "year", color_continuous_scale = px.colors.sequential.Plasma, projection = 'robinson')
In [21]:
# Animation with Mapbox choropleth

#px.choropleth_mapbox(dataset, locationmode="country names", locations="country",
#              color = "trade", hover_name = "country",
#           animation_frame = "year", color_continuous_scale = px.colors.sequential.Plasma, projection = 'robinson')

# Example from https://plot.ly/python/mapbox-county-choropleth/
from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)

import pandas as pd
#df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
df = pd.read_csv("fips-unemp-16-modified.csv",
                   dtype={"fips": str})

#df.head()

fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='unemp',
                           color_continuous_scale="Viridis",
                          range_color=(0, 12),
                          mapbox_style="carto-positron",
                           zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
                           opacity=0.5,
                           labels={'unemp':'unemployment rate'},
                           animation_frame="year"
                          )
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
In [59]:
# Small multiples of Maxbox choropleths
# https://plot.ly/python/mapbox-county-choropleth/

# Small multiples of 3 maps for 2016 to 2018
sm = make_subplots(rows=1, cols=3,
    specs=[[{"type": "choroplethmapbox"} , {"type": "choroplethmapbox"}, {"type": "choroplethmapbox"}
           ]])

# Divide dataset according to years
df16 = df.query("year==2016")
df17 = df.query("year==2017")
df18 = df.query("year==2018")

map16 = px.choropleth_mapbox(df16, geojson=counties, locations='fips', color='unemp',
                           color_continuous_scale="Viridis",
                          range_color=(0, 12),
                          mapbox_style="carto-positron",
                           zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
                           opacity=0.5,
                           labels={'unemp':'unemployment rate'})
#map16.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
#map16.update_layout(mapbox_style="carto-positron")
#map16.show()

map17 = px.choropleth_mapbox(df17, geojson=counties, locations='fips', color='unemp',
                           color_continuous_scale="Viridis",
                          range_color=(0, 12),
                         mapbox_style="carto-positron",
                           zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
                           opacity=0.5,
                           labels={'unemp':'unemployment rate'})
#map17.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
#map17.update_layout(mapbox_style="carto-positron")

map18 = px.choropleth_mapbox(df18, geojson=counties, locations='fips', color='unemp',
                           color_continuous_scale="Viridis",
                          range_color=(0, 12),
                          mapbox_style="carto-positron",
                           zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
                           opacity=0.5,
                           labels={'unemp':'unemployment rate'})
#map18.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

# Hack to make it work with make_subplots
sm16 = map16['data'][0]
sm17 = map17['data'][0]
sm18 = map18['data'][0]

sm.add_trace(sm16, row=1, col=1)
sm.add_trace(sm17, row=1, col=2)
sm.add_trace(sm18, row=1, col=3)

sm.update_layout(showlegend=False, title_text="Small Multiples of Mapbox Choropleths (2016-2018)",
                 mapbox_style="carto-positron",
                 mapbox2_style="carto-positron",
                 mapbox3_style="carto-positron",
                 #width = 1000,
                 mapbox_center = {"lat": 37.0902, "lon": -95.7129},
                 mapbox2_center = {"lat": 37.0902, "lon": -95.7129},
                 mapbox3_center = {"lat": 37.0902, "lon": -95.7129}
                 #height = 1000
                 , margin={"r":0,"t":0,"l":0,"b":0}
                )

#sm.for_each_trace(
#    lambda trace: trace.update(mapbox_style="carto-positron")
#)

#print(map16.layout)
#print(sm.layout)

sm.show()
In [7]:
# Small multiples of choropleth map

from plotly.subplots import make_subplots

# Small multiples of 6 maps for 2005 ... 2010
figs = make_subplots(rows=2, cols=3,
    specs=[[{"type": "choropleth"}, {"type": "choropleth"}, {"type": "choropleth"}],
            [{"type": "choropleth"}, {"type": "choropleth"}, {"type": "choropleth"}]],
)

# Divide dataset according to years
data2005 = dataset.query("year==2005")
data2006 = dataset.query("year==2006")
data2007 = dataset.query("year==2007")
data2008 = dataset.query("year==2008")
data2009 = dataset.query("year==2009")
data2010 = dataset.query("year==2010")

fig2005 = px.choropleth(data2005, locationmode="country names", locations="country",
                 color = "trade", hover_name = "country", 
                 color_continuous_scale = px.colors.sequential.Plasma, projection = 'robinson')

fig2006 = px.choropleth(data2006, locationmode="country names", locations="country",
                 color = "trade", hover_name = "country", 
                 color_continuous_scale = px.colors.sequential.Plasma, projection = 'robinson')

fig2007 = px.choropleth(data2007, locationmode="country names", locations="country",
                 color = "trade", hover_name = "country", 
                 color_continuous_scale = px.colors.sequential.Plasma, projection = 'robinson')

fig2008 = px.choropleth(data2008, locationmode="country names", locations="country",
                 color = "trade", hover_name = "country", 
                 color_continuous_scale = px.colors.sequential.Plasma, projection = 'robinson')

fig2009 = px.choropleth(data2009, locationmode="country names", locations="country",
                 color = "trade", hover_name = "country", 
                 color_continuous_scale = px.colors.sequential.Plasma, projection = 'robinson')

fig2010 = px.choropleth(data2010, locationmode="country names", locations="country",
                 color = "trade", hover_name = "country", 
                 color_continuous_scale = px.colors.sequential.Plasma, projection = 'robinson')

# Only possible thanks to this hack:
# https://github.com/plotly/plotly_express/issues/83#issuecomment-529167965
trace1 = fig2005['data'][0]
trace2 = fig2006['data'][0]
trace3 = fig2007['data'][0]
trace4 = fig2008['data'][0]
trace5 = fig2009['data'][0]
trace6 = fig2010['data'][0]

figs.add_trace(trace1, row=1, col=1)
figs.add_trace(trace2, row=1, col=2)
figs.add_trace(trace3, row=1, col=3)
figs.add_trace(trace4, row=2, col=1)
figs.add_trace(trace5, row=2, col=2)
figs.add_trace(trace6, row=2, col=3)

figs.update_layout(showlegend=False, title_text="Small Multiples (2005-2010)")

figs.show()
In [8]:
# Education
px.line(dataset, x="year", y="education", line_group="country", color="country", template="plotly_white",
       color_discrete_map = dict(Argentina = "#4daf4a", Germany = "#377eb8"),
        labels = dict(year = "Year", education = "Government expenditure on education (% of GDP)"),
        range_y = [0, 100],
        render_mode = 'svg'
       )
In [9]:
px.area(dataset, x="year", y="trade", line_group="country", color="country", template="plotly_white",
       color_discrete_map = dict(Argentina = "#4daf4a", Germany = "#377eb8"),
        labels = dict(year = "Year", trade = "Merchandise Trade (% of GDP)"),
        range_y = [0, 100]
       )
In [10]:
px.scatter_matrix(dataset, dimensions=["trade", "education", "year"], color="country")
In [ ]: